package org.jbit.news.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.jbit.news.biz.TopicBiz;
import org.jbit.news.biz.impl.TopicBizImpl;
import org.jbit.news.dao.BaseDao;
import org.jbit.news.dao.TopicDao;
import org.jbit.news.entity.News;
import org.jbit.news.entity.Topic;

public class TopicDaoImpl extends BaseDao implements TopicDao{
//获取所有主题 
	public List<Topic> getAllTopics() {
		String sql="select * from topic";
		ResultSet rs=this.exxcuteQuery(sql);
		List<Topic> list = new ArrayList<Topic>();
		try {
			while (rs.next()) {
				Topic topic=new Topic();
				topic.setTid(rs.getInt("tid"));
				topic.setTname(rs.getString("tname"));
				list.add(topic);
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll(connection, pstmt, rs);
		}
		return list;
	}
	//更新主题
	public int updateTopic( Topic topic) {
		String sql="update topic set tname=? where tid=?";
		int num=this.executeUpdate(sql ,topic.getTname(),topic.getTid());
		return num;
	}

	public List<Topic> fingTopicByName(String name) {
		String sql="select * from topic where tname=?";
		ResultSet rs=this.exxcuteQuery(sql,name);
		List<Topic> list1= new ArrayList<Topic>();
		try {
			while (rs.next()) {
				Topic topic=new Topic();
				topic.setTid(rs.getInt("tid"));
				topic.setTname(rs.getString("tname"));
				list1.add(topic);
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll(connection, pstmt, rs);
		}
		return list1;
	}
	//添加主题
	public int addTopic(String name) {
		String sql="insert into topic(tname) values(?)";
		int num=this.executeUpdate(sql,name);
		return num;
	}

	public int deleteTopic(int tid) {
		String sql="delete from topic where tid = ?";
		int num=this.executeUpdate(sql, tid);
		return num;
	}

}
